﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace CubeDetection
{
   [Serializable]
   public struct CubeFilterSettings
   {
      public string Filename;
      public rgb BorderColor;
      public int RasterX;
      public int RasterY;
      public Rectangle ROI;
   }
   [Serializable]
   public struct rgb
   {
      public byte b, g, r;
   }
   //public struct CubeInfo
   //{
   //   public Rectangle Cube;
   //   public Color Color;
   //   public bool Valid;
   //   public int BaseX, BaseY;
   //}
   public class CubeInfo
   {
      public Rectangle Cube;
      public Color Color;
      public bool Valid;
      public int BaseX, BaseY;
      
      public static int CompareCubeInfo(CubeInfo c1, CubeInfo c2)
      {
         if (c1.Cube.Width * c1.Cube.Height < c2.Cube.Width * c2.Cube.Height)
            return 1;
         else if (c1.Cube.Width * c1.Cube.Height > c2.Cube.Width * c2.Cube.Height)
            return -1;
         return 0;
      }
      public static int SortByPosition(CubeInfo c1, CubeInfo c2)
      {
         int IgnoreDistance = 35;
         
         if(Math.Abs(c2.Cube.Y-c1.Cube.Y) < IgnoreDistance)
         {
            //Auf gleicher Höhe
            if (c1.Cube.X < c2.Cube.X)
               return -1;
            if (c1.Cube.X > c2.Cube.X)
               return 1;
            return 0;
            }
         if (c1.Cube.Y < c2.Cube.Y)
            return -1;
         if (c1.Cube.Y > c2.Cube.Y)
            return 1;
         return 0;
      }
   }
   public class RGBStats
   {
      public List<rgb> List;
      public RGBStats()
      {
         List = new List<rgb>();
      }
      public void Add(rgb rgb)
      {
         //if (List.Count < 1)
            List.Add(rgb);
      }
      public Color MeanColor()
      {
         ulong r = 0, g = 0, b = 0;
         foreach (rgb c in List)
         {
            r += c.r;
            g += c.g;
            b += c.b;
         }
         return Color.FromArgb((int)(r / (ulong)List.Count),(int)( g / (ulong)List.Count), (int)(b / (ulong)List.Count));
      }
      public rgb Mean()
      {
         ulong r = 0, g = 0, b = 0;
         foreach (rgb c in List)
         {
            r += c.r;
            g += c.g;
            b += c.b;
         }
         rgb result = new rgb();
         result.r = (byte)(r / (ulong)List.Count);
         result.g = (byte)(g / (ulong)List.Count);
         result.b = (byte)(b / (ulong)List.Count);
         return result;
      }
      public static RGBStats operator + (RGBStats a,RGBStats b)
      {
         RGBStats result = new RGBStats();
         result.List.AddRange(a.List);
         result.List.AddRange(b.List);
         return result;
      }

   }
}
